package com.etop.weibo.service;

import com.etop.weibo.annotation.Daodi;
import com.etop.weibo.dao.UserDao;
import com.etop.weibo.util.DaoProperties;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jansing lz_jansing@163.com
 */
public class BaseService {

    public BaseService() {
        Method[] methods = this.getClass().getDeclaredMethods();
        for (Method m : methods) {
            if (m.isAnnotationPresent(Daodi.class)) {
                try {
                    String daoName = m.getAnnotation(Daodi.class).value();
                    if (daoName == null || daoName.trim().equals("")) {
                        daoName = m.getName().substring(3);
                        daoName = daoName.substring(0, 1).toLowerCase() + daoName.substring(1);
                    }
                    Object dao = DaoProperties.getDao(daoName);
                    m.invoke(this, dao);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(BaseService.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(BaseService.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(BaseService.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
